package com.bw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.common.ResponseResult;
import com.bw.entity.ZkUser;
import com.bw.service.IZkUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 莫晓晨
 * @since 2020-05-29
 */
@RestController
@RequestMapping("user")
@CrossOrigin
public class ZkUserController {

    @Autowired
    private IZkUserService userService;

    @PostMapping("login")
    public ResponseResult login(@RequestBody ZkUser zkUser){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            Map map = new HashMap<>();
            map.put("uname",zkUser.getUname());
            map.put("pwd",zkUser.getPwd());
            queryWrapper.allEq(map);

            List<ZkUser> list = userService.list(queryWrapper);

            if(list.size()>0){
                result.setResult(list.get(0));
                result.setMessage("登录成功");
            }else{
                result.setSuccess(false);
                result.setMessage("登录失败");
            }
        }catch (Exception e){
                result.setSuccess(false);
                result.setMessage("登录异常"+e.toString());
        }
        return result;
    }

}
